package com.itic.ht.mem.performance.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.applications.sys.code.vo.CodeDetailVo;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.exception.definition.ParameterErrorException;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.ht.cms.studyrecord.persistence.model.StudyRecord;
import com.itic.ht.mem.performance.persistence.model.Performance;
import com.itic.ht.mem.performance.persistence.model.PerformanceExample;
import com.itic.ht.mem.performance.persistence.model.PerformanceExample.Criteria;
import com.itic.ht.mem.performance.persistence.model.Performancedetail;
import com.itic.ht.mem.performance.persistence.model.PerformancedetailExample;
import com.itic.ht.mem.performance.service.PerformanceService;
import com.itic.ht.mem.performance.service.PerformancedetailService;

@Controller
@RequestMapping(value = "/mem/performance")
public class PerformanceController extends BaseController{
	
	@Autowired
	private PerformanceService performanceService;
	@Autowired
	private PerformancedetailService pdService;
	////////////////////////////////////////////////////// 常量区////////////////////////////////////////////////////////////////
	private static final String MODEL_NAME_CHINA = "我的业绩";// 中文名
	private static final String MODEL_NAME＿ENGLISH = "performance";// 英文名
	private static final String PAGE_COMMON_PATH = "/mem/performance/performance";// 通用路径
	private static final String COMMON_URL = "/mem/performance/";// 通用路径


	/*
	 * 主界面
	 */
	@RequestMapping(value = "/init")
	public String init(Model model) {
		model.addAttribute("name_china", MODEL_NAME_CHINA);
		model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
		model.addAttribute("common_url", COMMON_URL);
		return PAGE_COMMON_PATH + "Main";
	}

	/*
	 * 添加页面
	 */
	@RequestMapping(value = "/initAdd")
	public String initAdd(Model model) {
		model.addAttribute("name_china", MODEL_NAME_CHINA);
		model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
		model.addAttribute("common_url", COMMON_URL);
		return PAGE_COMMON_PATH + "Add";
	}
	 /*
     * 编辑页面
     */
    @RequestMapping(value="/initEdit")
    public String initEdit(String id , Model model){
    	if( StringHelper.isNotBlank(id) ) {
    		Performance temp = performanceService.get(id);
    		model.addAttribute("editData",iticJsonMapper.toJson(temp));
    		model.addAttribute("name_china", MODEL_NAME_CHINA);
        	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
        	model.addAttribute("common_url", COMMON_URL);
    	}else {
            throw new ParameterErrorException();
        }
    	return PAGE_COMMON_PATH+"Edit";
    }
	/*
	 * 详情页面
	 */
	@RequestMapping(value = "/initView")
	public String initView(String account, Model model) {
		if (StringHelper.isNotBlank(account)) {
			model.addAttribute("name_china", MODEL_NAME_CHINA);
			model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
			model.addAttribute("common_url", COMMON_URL);
			model.addAttribute("account", account);
		} else {
			throw new ParameterErrorException();
		}
		return PAGE_COMMON_PATH + "View";
	}

	@RequestMapping(value = "/initList")
	@ResponseBody
	public Map<String, Object> initView(String account, HttpServletRequest request) {
		// 声明返回对象
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (StringHelper.isNotBlank(account)) {
			PerformancedetailExample example = new PerformancedetailExample();
			example = this.handlePageQuery(request, example);
			// 封装分页参数
			example.createCriteria().andAccountEqualTo(account);
			example.setOrderByClause("CTIME desc");
			// 获取信息总条数
			int total = pdService.count(example);

			// 获取数据
			List<Performancedetail> list = pdService.list(example);


			// 数据返回处理
			resultMap.put(IticConst.TOTAL_RECORDS, total);
			resultMap.put(IticConst.DISPLAY_RECORDS, total);
			resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
			resultMap.put(IticConst.DATA, list);
		}

		return resultMap;
	}
	/**
     * 编辑保存
     * @param templet
     * @return
     */
    @RequestMapping(value="/edit")
    @ResponseBody
    public Map<String,String> edit(Performance templet){
    
    	 Map<String, String> resultMap = new HashMap<String, String>();
         String result = ResponseCode.SERVER_ERROR.getCode();
         result = performanceService.edit(templet);
         
         resultMap.put(IticConst.RESULT, result);
         return resultMap;
    }

	/**
	 * 添加模板管理数据
	 * 
	 * @param android
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Map<String, String> add(Performance templet) {

		Map<String, String> resultMap = new HashMap<String, String>();
		String result = ResponseCode.SERVER_ERROR.getCode();
		result = performanceService.add(templet);
		resultMap.put(IticConst.RESULT, result);
		return resultMap;
	}

	/**
	 * 删除
	 * 
	 */
	@RequestMapping(value = "/del")
	@ResponseBody
	public Map<String, String> del(String ids) {
		Map<String, String> resultMap = new HashMap<String, String>();
		String result = ResponseCode.SERVER_ERROR.getCode();
		if (StringHelper.isNotBlank(ids)) {
			Performance temp = new Performance();
			temp.setId(ids);
			temp.setEffective(DBConst.FALSE);
			result = performanceService.delete(temp);
		}

		resultMap.put(IticConst.RESULT, result);
		return resultMap;
	}

	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(Performance templet, HttpServletRequest request) {
		// 声明返回对象
		Map<String, Object> resultMap = new HashMap<String, Object>();

		PerformanceExample example = this.queryCondition(request, templet);
		// 封装分页参数
		example = this.handlePageQuery(request, example);

		// 获取信息总条数
		int total = performanceService.count(example);

		// 获取数据
		List<Performance> list = performanceService.list(example);


		// 数据返回处理
		resultMap.put(IticConst.TOTAL_RECORDS, total);
		resultMap.put(IticConst.DISPLAY_RECORDS, total);
		resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
		resultMap.put(IticConst.DATA, list);
		return resultMap;

	}

	private PerformanceExample queryCondition(HttpServletRequest request, Performance templet) {
		// 参数处理
		PerformanceExample example = new PerformanceExample();
		// 按照创建时间倒叙
		example.setOrderByClause("ctime desc");
		// 查询条件
		Criteria criteria = example.createCriteria().andEffectiveEqualTo(DBConst.TRUE);
		if (null != templet) {
			// 用户账号查询
			if (StringHelper.isNotBlank(templet.getMemAccount())) {
				criteria.andMemAccountEqualTo(templet.getMemAccount());
			}

		}
		return example;
	}

	/**
	 * 获取代码表信息 type :代码表类型，如T_DM_SEX filterCode :需要过滤的代码
	 * 
	 * @param codeDetailVo
	 * @return
	 */
	@RequestMapping(value = "/getCodeDetail")
	@ResponseBody
	public List<CodeDetailVo> getCodeDetail(CodeDetailVo codeDetailVo) {
		// 声明返回对象
		List<CodeDetailVo> codeDetailVoList = new ArrayList<CodeDetailVo>();
		// 封装数据
		CodeDetailVo vo = new CodeDetailVo();
		vo.setId("1");
		vo.setCode("1");
		vo.setName("通过");
		codeDetailVoList.add(vo);
		vo = new CodeDetailVo();
		vo.setId("0");
		vo.setCode("0");
		vo.setName("不通过");
		codeDetailVoList.add(vo);
		return codeDetailVoList;
	}
}
